package com.wzl.backend.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzl.backend.mapper.DayRecitationTableMapper;
import com.wzl.backend.pojo.DayRecitationTable;
import com.wzl.backend.pojo.Word;
import com.wzl.backend.service.DayRecitationTableService;
import com.wzl.backend.service.WordService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.*;

@Service
public class DayRecitationTableServiceImpl extends ServiceImpl<DayRecitationTableMapper , DayRecitationTable>
                                            implements DayRecitationTableService {

    @Autowired
    WordService wordService;

    @Override
    @Transactional
    public void studyToday(Long uId , String wordIdsJson) {
        //判断今天有没有背过单词
        QueryWrapper<DayRecitationTable> wrapper = new QueryWrapper<>();
        wrapper.eq("u_id" , uId);
        wrapper.eq("recitation_date" , new Date(System.currentTimeMillis()));
        DayRecitationTable dayRecitationTable = getOne(wrapper);
        DayRecitationTable table = new DayRecitationTable();

        //  如果今天没背单词，则新建一条数据，放入数据库
        if (Objects.isNull(dayRecitationTable)) {
            table.setRecitationDate(new Date(System.currentTimeMillis()));
            table.setUId(uId);
            table.setRecitationWord(wordIdsJson);
            save(table);

            //否则将新的单词加入到原有单词中
        } else {
            List<Long> newWordIds = JSONArray.parseArray(wordIdsJson, Long.class);
            List<Long> curWordIds = JSONArray.parseArray(dayRecitationTable.getRecitationWord(), Long.class);

            //set集合去重，如果当天学了重复的单词，则必须去重
            Set<Long> set = new TreeSet<>();
            set.addAll(newWordIds);
            set.addAll(curWordIds);

            curWordIds.addAll(newWordIds);
            table.setId(dayRecitationTable.getId());
            table.setRecitationWord(JSONArray.toJSONString(set));
            updateById(table);
        }
    }

    @Override
    public int getStudyWordCount(Long uId) {
        QueryWrapper<DayRecitationTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("recitation_date" , new Date(System.currentTimeMillis()));
        queryWrapper.eq("u_id" , uId);
        DayRecitationTable one = getOne(queryWrapper);
        List<Integer> integers;
        try {
            integers = JSONArray.parseArray(one.getRecitationWord(), int.class);
        } catch (Exception e) {
            return 0;
        }
        return integers.size();
    }

    @Override
    public int getAllStudyWordCount(Long uId) {
        QueryWrapper<DayRecitationTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u_id" , uId);
        List<DayRecitationTable> list = list(queryWrapper);
        int ans = 0;
        for (DayRecitationTable table : list) {
            String recitationWord = table.getRecitationWord();
            List<Integer> words = JSONArray.parseArray(recitationWord, int.class);
            ans += words.size();
        }
        return ans;
    }

    @Override
    public List<Word> learning(Long uId, int limit) {
        int wordCount = wordService.count();
        DayRecitationTable oneByuIdOrderByRecitationDate = this.baseMapper.getOneByuIdOrderByRecitationDate(uId);
        List<Integer> integers = null;
        List<Word> ans;
        try {
            integers = JSONArray.parseArray(oneByuIdOrderByRecitationDate.getRecitationWord(), int.class);
        } catch (Exception e) {
            ans = new ArrayList<>();
            for (int i = 1 ; i <= limit ; i++) {
                Word byId = wordService.getById(i);
                ans.add(byId);
            }
            return ans;
        }
        int lastWord = integers.get(integers.size() - 1);
        int count = 0;
        ans = new ArrayList<>();
        for (int i = lastWord + 1 ; count < limit ; i++,count++) {
            if (i == wordCount + 1) {
                i = 1;
            }
            Word byId = wordService.getById(i);
            ans.add(byId);
        }
        return ans;
    }

    @Override
    public List<Map<String , Object>> getAllStudyDate(Long id) {
        QueryWrapper<DayRecitationTable> wrapper = new QueryWrapper<>();
        wrapper.eq("u_id" , id);
        List<DayRecitationTable> list  = list(wrapper);
        List<Map<String , Object>> map = new ArrayList<>();
        for (DayRecitationTable day : list) {
            String json = day.getRecitationWord();
            List<Integer> words = JSONArray.parseArray(json, Integer.class);
            List<Word> wordList = wordService.listByIds(words);
            Map<String , Object> m = new HashMap<>();
            m.put("date" , day.getRecitationDate());
            m.put("data" , wordList);
            map.add(m);
        }
        return map;
    }

}
